package com.szyjjk.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szyjjk.entity.RunSiteHKObsExample;
import com.szyjjk.entity.SysImageCrawlerFiles;
import com.szyjjk.entity.SysImageCrawlerFilesExample;
import com.szyjjk.mapper.SysImageCrawlerFilesMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author yangyue
 * @Description //TODO 爬虫数据
 * @Date 11:36 2019/6/1
 * @Param 
 * @return 
 **/
@Service
@Transactional(readOnly = true)
public class SysImageCrawlerFilesService {


    @Autowired
    private SysImageCrawlerFilesMapper sysImageCrawlerFilesMapper;

    /**
     * @Author yangyue
     * @Description //TODO 爬虫数据分页查询
     * @Date 15:16 2019/5/22
     * @Param [pageNo, pageSize, begTime, endtime]
     * @return com.github.pagehelper.PageInfo<com.bkht.hyypt.hyybypt.entity.SysImageCrawler>
     **/
    public PageInfo<SysImageCrawlerFiles> findPageCrealwerFile(Integer pageNo, Integer pageSize, String crawlerID) {
        PageHelper.startPage(pageNo, pageSize);
        SysImageCrawlerFilesExample sysImageCrawlerFilesExampleExample = new SysImageCrawlerFilesExample ();
        //按时间分页
//        if (begTime!=null) {
//            sysImageCrawlerFilesExampleExample.createCriteria().andFileDownloadTimeBetween(begTime,endtime);
//        }else{
//        //筛选下载成功的
//            sysImageCrawlerFilesExampleExample.createCriteria().andFileStateEqualTo(1);
//        }
        if(crawlerID==null){
            sysImageCrawlerFilesExampleExample.createCriteria().andFileStateEqualTo(1);
        }else{
            sysImageCrawlerFilesExampleExample.createCriteria().andCrawlerDownIdEqualTo(crawlerID);
        }
        sysImageCrawlerFilesExampleExample.setOrderByClause("file_download_time desc");
        Page<SysImageCrawlerFiles> sysProductPage = (Page<SysImageCrawlerFiles>) sysImageCrawlerFilesMapper.selectByExample(sysImageCrawlerFilesExampleExample);

        return new PageInfo<>(sysProductPage.getResult());
    }


    /**
     * @Author yangyue
     * @Description //TODO 查询一个爬虫数据文件
     * @Date 17:32 2019/4/24
     * @Param [id]
     * @return com.bkht.hyypt.hyybypt.entity.SysImageCrawlerFiles
     **/
    public SysImageCrawlerFiles findByIdCrawlerFiles(String id) {
        return sysImageCrawlerFilesMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author yangyue
     * @Description //TODO 删除爬虫数据文件
     * @Date 17:30 2019/4/24
     * @Param [id]
     * @return java.lang.Boolean
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean deleteCrawlerFiles(String id) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerFilesMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @Author yangyue
     * @Description //TODO 修改爬虫数据文件
     * @Date 17:30 2019/4/24
     * @Param [sysImageCrawlerFiles]
     * @return java.lang.Boolean
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean editCrawlerFiles(SysImageCrawlerFiles sysImageCrawlerFiles) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerFilesMapper.updateByPrimaryKeySelective(sysImageCrawlerFiles);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }





    /**
     * @Author yangyue
     * @Description //TODO 添加爬虫数据文件
     * @Date 17:18 2019/4/24
     * @Param [sysProduct]
     * @return java.lang.Boolean
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveCrawlerFiles(SysImageCrawlerFiles sysImageCrawlerFiles) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerFilesMapper.insertSelective(sysImageCrawlerFiles);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @Author yangyue
     * @Description //TODO 今天是否已经下载了存在数据  flas 没数据  true 有数据
     * @Date 11:41 2019/6/17
     * @Param []
     * @return boolean
     **/
    public List<SysImageCrawlerFiles> findTodayData(String id){
        SysImageCrawlerFilesExample sysImageCrawlerFilesExampleExample = new SysImageCrawlerFilesExample ();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        Date zerotow = calendar.getTime();
        sysImageCrawlerFilesExampleExample.createCriteria().andCrawlerDownIdEqualTo(id).andFileDownloadTimeBetween(zero,zerotow);
        sysImageCrawlerFilesExampleExample.setOrderByClause("file_download_time desc");
        List<SysImageCrawlerFiles> ar =  sysImageCrawlerFilesMapper.selectByExample(sysImageCrawlerFilesExampleExample);
        return ar;
    }



    /**
     * @Author yangyue
     * @Description //TODO 查出该任务下最新的数据
     * @Date 9:48 2019/6/27
     * @Param [crawlerId]
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawlerFiles>
     **/
    public List<SysImageCrawlerFiles> getNewDataCrawlerFiles(String crawlerId){
        //首先查询出最新的一个数据
        SysImageCrawlerFilesExample example  = new SysImageCrawlerFilesExample ();
        SysImageCrawlerFilesExample.Criteria criteria = example.createCriteria();
        criteria.andCrawlerDownIdEqualTo(crawlerId);
        example.setOrderByClause("file_download_time desc");
        List<SysImageCrawlerFiles> arCrawler_list =  sysImageCrawlerFilesMapper.selectByExample(example);
        SysImageCrawlerFiles sysImageCrawlerFilesInfo=arCrawler_list.get(0);
        //获取他的日期
        Date fistTime=sysImageCrawlerFilesInfo.getFileDownloadTime();
        //在获取这个日期下所有数据
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fistTime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date zerotow = calendar.getTime();

        example  = new SysImageCrawlerFilesExample ();
        example.createCriteria().andCrawlerDownIdEqualTo(crawlerId).andFileDownloadTimeBetween(zero,zerotow);
        List<SysImageCrawlerFiles> ar =  sysImageCrawlerFilesMapper.selectByExample(example);
        return ar;
    }



    /**
     * @Author yangyue
     * @Description //TODO 根据workID 和
     * @Date 15:18 2019/7/4
     * @Param [crawlID, fileName]
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawlerFiles>
     **/
    public List<SysImageCrawlerFiles> getCrawlerFileByFileName(String crawlID,String fileName){
        SysImageCrawlerFilesExample example=new SysImageCrawlerFilesExample();
        example.createCriteria().andCrawlerDownIdEqualTo(crawlID).andFileNameLike(fileName);
//        example.setOrderByClause("file_download_time desc");
        return  sysImageCrawlerFilesMapper.selectByExample(example);
    }


    /**
     * @Author yangyue
     * @Description //TODO 根据时间查询的历史记录
     * @Date 11:04 2020/5/9
     * @Param [crawlID, beginTime, endTime]
     * @return java.util.List<com.szyjjk.entity.SysImageCrawlerFiles>
     **/
    public List<SysImageCrawlerFiles> getPageCrawlerFileByTme(Integer pageNo, Integer pageSize,String crawlID,Date beginTime,Date endTime){

        PageHelper.startPage(pageNo, pageSize);
        SysImageCrawlerFilesExample example=new SysImageCrawlerFilesExample();
        if (StringUtils.isNotEmpty(crawlID)) {
            if (beginTime == null) {
                example.createCriteria().andCrawlerDownIdEqualTo(crawlID);
            } else {
                example.createCriteria().andCrawlerDownIdEqualTo(crawlID).andFileDownloadTimeBetween(beginTime,endTime);
            }
            example.setOrderByClause("file_download_time desc limit " + pageNo + " , " + pageSize);
        }
        return sysImageCrawlerFilesMapper.selectByExample(example);
 
    }

    /**
     * @Author yangyue
     * @Description //TODO 返回数量
     * @Date 14:40 2020/5/9
     * @Param [crawlID, begTime, endTime]
     * @return int
     **/
    public int findPageImageCount(String crawlID, Date begTime, Date endTime) {
        SysImageCrawlerFilesExample example=new SysImageCrawlerFilesExample();
        if (StringUtils.isNotEmpty(crawlID)) {
            if (begTime == null) {
                example.createCriteria().andCrawlerDownIdEqualTo(crawlID);
            } else {
                example.createCriteria().andCrawlerDownIdEqualTo(crawlID).andFileDownloadTimeBetween(begTime,endTime);
            }
        }
        return sysImageCrawlerFilesMapper.selectByExample(example).size();
    }


    /**
     * @Author yangyue
     * @Description //TODO 查询所有
     * @Date 14:46 2019/9/19
     * @Param []
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawlerFiles>
     **/
    public List<SysImageCrawlerFiles> getAllfiles(){
        return  sysImageCrawlerFilesMapper.selectByExample(null);
    }


    /**
     * @Author yangyue
     * @Description //TODO 更新
     * @Date 14:56 2019/9/19
     * @Param [sysImageCrawlerFiles]
     * @return void
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean updateCrawlerFiles(SysImageCrawlerFiles sysImageCrawlerFiles){

        try {
            //todo 可填加后续逻辑
            sysImageCrawlerFilesMapper.updateByPrimaryKey(sysImageCrawlerFiles);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }




}
